<html>
<head><meta charset="utf-8"><title>Branch prediction for Option / Result · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Branch.20prediction.20for.20Option.20.2F.20Result.html">Branch prediction for Option / Result</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="242915690"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Branch%20prediction%20for%20Option%20/%20Result/near/242915690" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Branch.20prediction.20for.20Option.20.2F.20Result.html#242915690">(Jun 16 2021 at 17:38)</a>:</h4>
<p>During training, someone asked if there's any pre-existing branch prediction hints for <code>Option</code> / <code>Result</code>. I told them about the likely intrinsics and some of the RFCs in that area, but they were also asking if anything assumes that <code>Option</code> is usually <code>Some</code> or <code>Result</code> is usually <code>Ok</code>. </p>
<p>My understanding is that there isn't, but I'd be happy to be corrected.</p>



<a name="242916009"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Branch%20prediction%20for%20Option%20/%20Result/near/242916009" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Branch.20prediction.20for.20Option.20.2F.20Result.html#242916009">(Jun 16 2021 at 17:41)</a>:</h4>
<p>there isn't, but if you <code>.unwrap()</code> them or panic on one branch the panic invocation informs branch likelihood</p>



<a name="242916079"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Branch%20prediction%20for%20Option%20/%20Result/near/242916079" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Branch.20prediction.20for.20Option.20.2F.20Result.html#242916079">(Jun 16 2021 at 17:41)</a>:</h4>
<p>Panic implies unlikely?</p>



<a name="242916170"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Branch%20prediction%20for%20Option%20/%20Result/near/242916170" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Branch.20prediction.20for.20Option.20.2F.20Result.html#242916170">(Jun 16 2021 at 17:42)</a>:</h4>
<p>any call to a <code>#[cold]</code> function can inform branch weights</p>



<a name="242916201"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Branch%20prediction%20for%20Option%20/%20Result/near/242916201" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Branch.20prediction.20for.20Option.20.2F.20Result.html#242916201">(Jun 16 2021 at 17:42)</a>:</h4>
<p>panics are one of them</p>



<a name="242916406"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Branch%20prediction%20for%20Option%20/%20Result/near/242916406" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Branch.20prediction.20for.20Option.20.2F.20Result.html#242916406">(Jun 16 2021 at 17:44)</a>:</h4>
<p>Cool (pun unintended), thank you!</p>



<a name="242931209"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Branch%20prediction%20for%20Option%20/%20Result/near/242931209" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Branch.20prediction.20for.20Option.20.2F.20Result.html#242931209">(Jun 16 2021 at 19:35)</a>:</h4>
<p>Check out the <code>cold</code> function attribute in LLVM <a href="https://llvm.org/docs/LangRef.html#function-attributes">https://llvm.org/docs/LangRef.html#function-attributes</a> which Rust's <code>#[cold]</code> generates.  Any BB that dominates a call to something that's cold is treated as cold -- so any branch where one side leads to a panic eventually gets the other side treated as likely.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>